//
//  RestaurantDetailViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/19.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "RestaurantDetailViewController.h"
#import "ZWYNetTool.h"
#import "HXMacro.h"
#import "NSString+Vali.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "ResturantDetailModel.h"
#import "ResturantDetailRes.h"
#import "MasterComment.h"
#import "ResturantTextTableViewCell.h"
#import "ResturantDetailTableViewCell.h"
#import "ResturantOtherTableViewCell.h"
#import "SelfSizeTool.h"


#define kResturantTextTableViewCellName @"ResturantTextTableViewCellNameReuse"
#define kResturantDetailTableViewCellName @"ResturantDetailTableViewCellNameReuse"
#define kResturantOtherTableViewCellName @"ResturantOtherTableViewCellNameReuse"
@interface RestaurantDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *pic;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *num;
@property (weak, nonatomic) IBOutlet UILabel *resturantTitle;
@property (weak, nonatomic) IBOutlet UILabel *shortComment;
@property (nonatomic, strong) ResturantDetailModel *model;
@property (weak, nonatomic) IBOutlet UIView *backView;

@end

@implementation RestaurantDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _pic.contentMode = UIViewContentModeScaleAspectFill;
    _pic.clipsToBounds = YES;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    NSString *bodyString = [NSString stringWithFormat:@"id=%@", _numString];
    NSString *numString = [NSString stringWithFormat:@"%ld", [bodyString length]];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : numString};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=restaurant&a=getinfo" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [ResturantDetailModel modelWithDic:result];
        [_pic sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_model.res.mainpic]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
        _resturantTitle.text = _model.res.title;
        _shortComment.text = _model.res.shortinstruction;
        _num.text = [NSString stringWithFormat:@"%ld", _model.res.mainpicNum];
        [_headerView addSubview:_backView];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    [_tableView registerNib:[UINib nibWithNibName:@"ResturantTextTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kResturantTextTableViewCellName];
    [_tableView registerNib:[UINib nibWithNibName:@"ResturantDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kResturantDetailTableViewCellName];
    [_tableView registerNib:[UINib nibWithNibName:@"ResturantOtherTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kResturantOtherTableViewCellName];
    
}
- (IBAction)pop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger num = 0;
    if (_model.res.mastercomment.count) {
        if (section == 0) {
            num = _model.res.mastercomment.count;
        } else {
            num = 1;
        }
    } else {
        num = 1;
    }
    return num;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_model.res.mastercomment.count) {
        if (indexPath.section == 0) {
            return [SelfSizeTool selfSizeHeightWith:_model.res.mastercomment[indexPath.row].short_content width:self.view.h_width - 30 font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.mastercomment[indexPath.row].title width:self.view.h_width font:[UIFont systemFontOfSize:17]] + 30;
        } else if (indexPath.section == 1) {
            return [SelfSizeTool selfSizeHeightWith:_model.res.avgpriceshow width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.address width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.openshop width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.tel width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.avgpriceshow width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + 70;
        } else {
            return SCREEN_WIDTH / 3 + 50;
        }
    } else {
        if (indexPath.section == 0) {
            return [SelfSizeTool selfSizeHeightWith:_model.res.avgpriceshow width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.address width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.openshop width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.tel width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + [SelfSizeTool selfSizeHeightWith:_model.res.avgpriceshow width:self.view.h_width * 11 / 16 - 62  font:[UIFont systemFontOfSize:17]] + 70;
        } else {
            return SCREEN_WIDTH / 3 + 50;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_model.res.mastercomment.count) {
        if (indexPath.section == 0) {
            ResturantTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResturantTextTableViewCellName];
            cell.masterComment = _model.res.mastercomment[indexPath.row];
            return cell;
        } else if (indexPath.section == 1) {
            ResturantDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResturantDetailTableViewCellName];
            cell.res = _model.res;
            return cell;
        } else {
            ResturantOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResturantOtherTableViewCellName];
            cell.array = _model.res.more;
            return cell;
        }
    } else {
        if (indexPath.section == 0) {
            ResturantDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResturantDetailTableViewCellName];
            cell.res = _model.res;
            return cell;
        } else {
            ResturantOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResturantOtherTableViewCellName];
            cell.array = _model.res.more;
            return cell;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger num = 1;
    if (_model.res.mastercomment.count) {
        num += 1;
    }
    if (_model.res.more) {
        num += 1;
    }
    return num;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *string = [NSString new];
    if (_model.res.mastercomment.count) {
        if (section == 0) {
            string = @"大师点评";
        } else if (section == 1) {
            string = @"餐厅信息";
        } else {
            string = @"其他餐厅";
        }
    } else {
        if (section == 0) {
            string = @"餐厅信息";
        } else if (section == 1) {
            string = @"其他餐厅";
        }
    }
    
    return string;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
